// Fill out your copyright notice in the Description page of Project Settings.


#include "Character/Core/AnimatorComponent.h"
#include "PaperFlipbookComponent.h"
#include "PaperFlipbook.h"


UAnimatorComponent::UAnimatorComponent()
{
	PrimaryComponentTick.bCanEverTick = true;

}



void UAnimatorComponent::BeginPlay()
{
	Super::BeginPlay();
	Sprite =  GetOwner()->FindComponentByClass<UPaperFlipbookComponent>();
}



void UAnimatorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

	// EAnimDirection = DetermineDirection();

}

void UAnimatorComponent::SetDirection(FVector2D Direction)
{
	
	
}

void UAnimatorComponent::LookAt(FVector LookPoint)
{
	const FVector SelfPosition = GetOwner()->GetActorLocation();
	const FVector DirectionVector = SelfPosition - LookPoint;
	const EAnimDirection Direction = DetermineDirection(DirectionVector);
	
	if (CurrentAnimState == Idle)
	{
		switch (Direction)
		{
		case Down:
			Sprite->SetFlipbook(IdleDown);
			break;
		case Left:
			Sprite->SetFlipbook(IdleLeft);
			break;
		case Right:
			Sprite->SetFlipbook(IdleRight);
			break;
		case Up:
			Sprite->SetFlipbook(IdleUp);
			break;
		}
	}
}

EAnimDirection UAnimatorComponent::DetermineDirection(const FVector& Position)
{
	FVector2D Point = FVector2D(Position.X, Position.Y);	
	const int Quadrant = DetermineQuadrant(Point);
	Point = Point.GetAbs();
	if (Quadrant == 0)
	{
		return Point.X > Point.Y ? Up : Left;
	}
	if (Quadrant == 1)
	{
		return Point.X > Point.Y ? Down : Left;
	}
	if (Quadrant == 2)
	{
		return Point.X > Point.Y ? Down : Right;
	}
	return Point.X > Point.Y ? Up : Right;
}

int UAnimatorComponent::DetermineQuadrant(const FVector2D& Point)
{
	if (Point.X > 0.f)
	{
		return Point.Y < 0.0f ? 0 : 3;
	}
	return Point.Y < 0.0f ? 1 : 2;
}



